<template> 
  <div>
    <el-upload :action="useOss ? ossUploadUrl + '' : minioUploadUrl + ''" list-type="picture" :multiple="false"
      :show-file-list="showFileList" :on-progress="onProgress" :file-list="fileList"
      :headers="{ 'Authorization': token }" :before-upload="beforeUpload" :on-remove="handleRemove" :on-error="error"
      :on-success="handleUploadSuccess" :on-preview="handlePreview">
      <el-button size="s米糕" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png,mp4文件，且不超过10MB</div>
    </el-upload>
    <el-progress v-show="showProcess" :percentage="processLen" :color="customColor" />
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="fileList[0].url" alt="">
    </el-dialog>

    <el-dialog :visible.sync="videoVisible">
      <video width="100%" autoplay="true" controls="true" :src="fileList[0].url" alt=""></video>
    </el-dialog>
  </div>
</template>
<script>
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'
import { FILE_TYPE_VISIABLE } from "../../fileType/fileType"
import { deleteImage } from "../../api/oss"
export default {
  name: 'singleUpload',
  props: {
    replaceImage: {
      type: Function,
      default: (file) => {
        deleteImage(file.url);
      }
    },
    value: String,
    ossUploadUrl: {
      type: String,
      default: `${process.env.VUE_APP_SERVER}/aliyun/oss/uploadPic`
    },
  },
  computed: {
    imageUrl() {
      return this.value;
    },
    imageName() {
      if (this.value != null && this.value !== '') {
        return this.value.substr(this.value.lastIndexOf("/") + 1);
      } else {
        return null;
      }
    },
    fileList() {
      return [{
        name: this.imageName,
        url: this.imageUrl
      }]
    },
    showFileList: {
      get: function () {
        return this.value !== null && this.value !== '' && this.value !== undefined;
      },
      set: function (newValue) {
      }
    }
  },
  data() {
    return {
      dataObj: {
        policy: '',
        signature: '',
        key: '',
        ossaccessKeyId: '',
        dir: '',
        host: '',
        // callback:'',
        token: ""
      },
      customColor: "#000FAD",
      processLen: 0,
      videoVisible: false,
      dialogVisible: false,
      showProcess: false,
      useOss: true, //使用oss->true;使用MinIO->false
      minioUploadUrl: 'http://localhost:8080/minio/upload',
    };
  },

  created() {
    this.token = getToken();
  },
  methods: {



    onProgress(event, file, fileList) {

      let second = file.size / 1024000;

      let intervalSecond = second / 100 * 1000;
      console.log("intervalSecond", intervalSecond);
      this.showProcess = true;

      let intvalId = setInterval(() => {

        if ("success" == file.status) {
          this.processLen = 100;
          this.showProcess = false;
          clearInterval(intvalId);
          return;
        }

        if (this.processLen < 100) {
          this.processLen++;
          return;
        }


      }, intervalSecond);
      console.log(event, file, fileList)

    },

    error(err, file, fileList) {
      Message({
        message: err,
        type: 'error',
        duration: 3 * 1000
      })
    },

    emitInput(val) {
      console.log("val", val)
      this.$emit('input', val)
    },
    handleRemove(file, fileList) {
      this.emitInput('');
    },
    handlePreview(file) {

      let suffix = file.name.substr(file.name.indexOf("."));

      if (!FILE_TYPE_VISIABLE.hasOwnProperty(suffix)) {
        return;
      }

      this[FILE_TYPE_VISIABLE[suffix]] = true;

    },
    beforeUpload(file) {
      return true;
    },



    handleUploadSuccess(res, file) {

      Message({
        message: res.message,
        duration: 3 * 1000
      })

      if (500 == res.status) {
        return;
      }


      console.log("ossUploadUrl", this.ossUploadUrl, "file", file)

      this.showFileList = true;

      let image = this.fileList.pop();

      this.replaceImage(image);

      this.fileList.push({ name: file.name, url: res.data });
      console.log("this.fileList", this.fileList);
      this.emitInput(this.fileList[0].url);

      this.$emit("fileReceive", file);
    }
  }
}
</script>
<style>

</style>
